/**
 * une nouvelle version plus porpre pour les apollonian gasket
 */


import ij.ImagePlus;
import ij.plugin.PlugIn;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;


import java.awt.Color;


import java.awt.Font;

import java.awt.Polygon;
import java.io.PrintStream;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.util.Random; 
import java.util.TreeSet;



import pluginUtils.AGCircle;
import pluginUtils.AGCircleComparator;
import pluginUtils.AGCircleComparatorComplete;
import pluginUtils.ApollonianGasker;
import pluginUtils.Circle;
import pluginUtils.CircleComparator;
import pluginUtils.CircleInversion;
import pluginUtils.Complexe; 
import pluginUtils.MobiusTransform;
import pluginUtils.Point2D;
import pluginUtils.SoddyTriplet;



public class NewApollonianGasket2 extends ImagePlus implements PlugIn{
	{Locale.setDefault(Locale.US); }
	
	private ImageProcessor primeIP; 
	private int X=10000,Y=10000; // image size
	
	private PrintStream output;
	private double xShift=0; 
	private double zShift=0; 
	
	private TreeSet<AGCircle> alreadyDrawn=new TreeSet<AGCircle>(new AGCircleComparatorComplete());
 
	private double minRad=0.005; 
	private int compte=0; 
	// Le nombre de niveau de remplissage des interstices (dans makeGasket)
	private int level2=7; 
	

	private Random generator=new Random(); // One always needs some randomness
	
	// scaling the image
	private double x0=0,x1=1,y0=0,y1=1;
	private double acoef,ccoef,vcoef,wcoef; 

	
	
	public void setCoefs(){
		acoef=X/(x1-x0); 
		vcoef=Y/(y1-y0); 
		ccoef=-acoef*x0; 
		wcoef=-vcoef*y0; 
	}
	 
	
	
	public void setBounds(double xl,double xu,double yl,double yu){
		x0=xl; 
		x1=xu;
		y0=yl;
		y1=yu; 
	}
	
	private void printCircleDescription(AGCircle c){
		//if(intersect(c))return; 
		
		//if(c.getRadius()<0.1) return; 
		int intX=(int)(c.getCenter().getReal()*acoef+ccoef);
		int intY=(int)(c.getCenter().getIm()*vcoef+wcoef);
		int intRx=(int)(c.getRadius()*acoef);
		int intRy=(int)(c.getRadius()*vcoef);
		
	
			
			double Xs=c.getCenter().getReal(); 
			double Ys=c.getCenter().getIm(); 
			double rapp=Xs*Xs+Ys*Ys+1; 
			double u=Xs/rapp; 
			double v=Ys/rapp; 
			double w=(Xs*Xs+Ys*Ys)/rapp;
			
			double Xsr=c.getCenter().getReal(); 
			double Ysr=c.getCenter().getIm()+c.getRadius(); 
			double rappr=Xsr*Xsr+Ysr*Ysr+1; 
			double ur=Xsr/rappr; 
			double vr=Ysr/rappr; 
			double wr=(Xsr*Xsr+Ysr*Ysr)/rappr;
		
			double nrad=(u-ur)*(u-ur)+(v-vr)*(v-vr)+(w-wr)*(w-wr);
			//nrad=Math.sqrt(nrad)*1.414; 
			
			String s="sphere{<"+u+","+v+","+w+">,";
			
			
			s+=nrad; 
			s+=" texture{T1} finish{F1}}"; 
			output.println(s);
		
	}
		
	
	private void drawCircle(AGCircle c){
		double xCenter=c.getCenter().getReal(); 
		double yCenter=c.getCenter().getIm(); 
		//if(c.getRadius()<0.001) return ; 
		
		int intX=(int)((xCenter-xShift)*acoef+ccoef);
		int intY=(int)((yCenter-zShift)*vcoef+wcoef);
		int intRx=(int)(c.getRadius()*acoef);
		int intRy=(int)(c.getRadius()*vcoef);
		
		primeIP.setColor(Color.WHITE);
		primeIP.drawOval(intX-intRx,intY-intRy,intRx*2,intRy*2);
	}
		
		
	private void drawPoint(double x,double y){
		int radi=15; 
		int intX=(int)(x*acoef+ccoef);
		int intY=(int)(y*vcoef+wcoef);
		primeIP.fillOval(intX-radi,intY-radi,2*radi,2*radi);
	}
	
	
	
	private void drawTriangle(double[] X,double[] Y){
		int xint[]=new int[3]; 
		int yint[]=new int[3];
		for(int i=0;i<3;i++){
			xint[i]=(int)(X[i]*acoef+ccoef); 
			yint[i]=(int)(Y[i]*vcoef+wcoef);
		}
		primeIP.fillPolygon(new Polygon(xint,yint,3)); 
		
	}
	
	
	
	private void drawLine(double xd,double yd,double xf,double yf){
		int XD=(int)(xd*acoef+ccoef); 
		int YD=(int)(yd*vcoef+wcoef); 
		int XF=(int)(xf*acoef+ccoef); 
		int YF=(int)(yf*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	
	private void drawLine(Point2D p,Point2D q){
		int XD=(int)(p.getX()*acoef+ccoef); 
		int YD=(int)(p.getY()*vcoef+wcoef); 
		int XF=(int)(q.getX()*acoef+ccoef); 
		int YF=(int)(q.getY()*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	/*
	ArrayList<SoddyTriplet> makeTriplets(AGCircle a,AGCircle b,AGCircle c,AGCircle d){
		ArrayList<SoddyTriplet> s=new ArrayList<SoddyTriplet>();
		s.add(new SoddyTriplet(a, b, c)); 
		s.add(new SoddyTriplet(a, c, d)); 
		s.add(new SoddyTriplet(a, d, b));
		s.add(new SoddyTriplet(b, c, d)); 
		return s; 
	}
	*/
	/** Construit des soddy triplets a partir d'un cercle qui devient le cercle exterieur
	 * 
	 */
	ArrayList<SoddyTriplet> makeListe(AGCircle origin){
		ArrayList<SoddyTriplet> pro=new ArrayList<SoddyTriplet>();
		double dx=origin.getXcenter(); 
		double dy=origin.getYcenter(); 
		AGCircle myOrigin=new AGCircle(origin.getCenter(),origin.getRadius(),true);
		AGCircle ring[]=new AGCircle[3];
		double petitr=origin.getRadius()*(Math.sin(Math.PI/3)/(1+Math.sin(Math.PI/3)));
		
		for(int i=0;i<3;i++){
			ring[i]=new AGCircle(Complexe.createComplexeFromRI(dx+(origin.getRadius()-petitr)*Math.cos(2*Math.PI*i/3),dy+(origin.getRadius()-petitr)*Math.sin(2*Math.PI*i/3 )), petitr,false);
		}
		for(int i=0;i<3;i++){
			pro.add(new SoddyTriplet(myOrigin, ring[i], ring[(i+1)%3]));
		}
			pro.add(new SoddyTriplet(ring[0],ring[1],ring[2])); 
			return pro; 
	}
	
	protected void gasketize(AGCircle c,int level){
		if(c.getRadius()<minRad) return;
		if(level==0) {
			if(!alreadyDrawn.contains(c)){
			drawCircle(c);
			compte++;
			if(compte%10000==0){
				System.out.println(compte); 
				updateAndDraw(); 
			}
			alreadyDrawn.add(c);
			}
			
			
			
		
			return;
			}
		//if(level==1)drawCircle(c); 
		
		
		ArrayList<SoddyTriplet> maliste=makeListe(c);
		// TODO : construire une liste de ST relatifs a une chaine de Steiner
		for(SoddyTriplet st: maliste){ 
			ApollonianGasker maker=new ApollonianGasker(st,level2,minRad); 
			maker.makeGasket(); 
			Iterator<AGCircle> suivants=maker.iterator(); 
			while(suivants.hasNext()){
				gasketize(suivants.next(),level-1); 
			}
		}
	}
		

	public void run(String arg) {
		primeIP = new ColorProcessor(X,Y);
		
		
		
		
		double cwe=2*Math.sqrt(3)/3;
		double decale=10;  
	
		
		
		double eps=0.1;
		
		setBounds(10-(1+cwe)-eps,10+(1+cwe)+eps,-(1+cwe)-eps,1+cwe+eps);
	
		setCoefs(); 
	
		Polygon background=new Polygon(new int[]{0,0,X,Y},new int[]{0,X,Y,0},4);
		primeIP.setColor(Color.BLUE);
		
		primeIP.fillPolygon(background);
		
		
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.BLACK);
		primeIP.setLineWidth(1); 
		

		setProcessor("Verifying", primeIP);
		
	
		primeIP.setColor(Color.BLACK);
		primeIP.setLineWidth(2);
		
		
		show(); 
		
		

		AGCircle zero=new AGCircle(Complexe.createComplexeFromRI(decale,0),1+cwe,true); 
		// le nombre de fois ou on rentre dans un cercle pour y creer un AG
		int level=7; 
		drawCircle(zero);

		gasketize(zero,level);
		System.out.println("fini"); 
		System.out.println(alreadyDrawn.size()); 
		//for(AGCircle c: alreadyDrawn) drawCircle(c); 
		updateAndDraw(); 
		show();
		
	}
		
}


